<?php
/**
 * Created by JetBrains PhpStorm.
 * User: T420
 * Date: 9/27/12
 * Time: 3:58 PM
 * To change this template use File | Settings | File Templates.
 */
class Controllers_Shoppingcart extends Libs_Controller
{

    private $userid;
    public function __construct()
    {
        parent::__construct();
        $this->model=new Models_ShoppingCart();
        $this->menu=new Models_Menu();
        $this->getmenu();
        //$this->model=new Models_Product();

    }
    public function getmenu()
    {

        $this->view->listmenusys=$this->menu->getmenusystem();
        $this->view->listmenuproduct=$this->menu->getmenuproduct();
        $this->view->listmanufacture=$this->menu->getmanufactory();
        $this->view->listnews=$this->menu->getnews();
    }


    public function index()
    {
        if(isset($_SESSION['username'])&&$_SESSION['username']!='')
        {
            $list=$this->model->getintrouser($_SESSION['username']);
            foreach($list as $key)
            {
                $this->userid=$key->userid;
                $this->view->username=$key->username;

                $this->view->email=$key->email;
                $this->view->phone=$key->phone;
            }
        }
        if($_SESSION['cart'])
        {
            foreach($_SESSION['cart'] as $key=>$value)
            {
                $item[]=$key;
            }
            $str=implode(",",$item);



//
            $this->view->listcart=$this->model->getlistcart($str);//lay thong tin ve san pham

            $this->view->listpay=$this->model->getpayment();//Lay cac hinh thuc dat hang
        }

        $this->view->render("shoppingcart/index");
    }
    public function add($param)
    {

        if($param[0])
        {
            $id=$param[0];
            if(isset($_SESSION['cart'][$id]))
            {
                $qty=$_SESSION['cart'][$id]+1;
            }
            else{
                $qty=1;
            }
            $_SESSION['cart'][$id]=$qty;
            $_SESSION['tongsp']+=1;
        }

       // $this->view->render('shoppingcart/index');
        //header("location:'/mymvc/shoppingcart'");
       // $this->index();
        echo '<script type="text/javascript">history.back()</script>';

    }
    public function del($param)
    {
        $id=$param[0];
        if($id==0)
        {
            unset($_SESSION['cart']);
        }
        else
        {
            unset($_SESSION['cart'][$id]);
        }

       // header("location:/mymvc/shoppingcart");
        echo '<script type="text/javascript">history.back()</script>';
    }
    public function update()
    {
        if(isset($_POST['update']))
        {
            foreach($_POST['qty'] as $key=>$value)
            {
                if($value>0 and (is_numeric($value)))
                $_SESSION['cart'][$key]=round($value);
            }
        }
//        if(isset($_POST['payment']) and $_SESSION['username']!='')
//        {
//            $user=new Models_ShoppingCart();
//            $this->view->introuser= $user->getintrouser($_SESSION['username']);
//            $captchar=new captcha();
//            $this->view->listpay=$user->getpayment();
//        }
        //header("location:/mymvc/shoppingcart");
       echo '<script type="text/javascript">history.back()</script>';
    }
    public function payment()
    {
        $this->index();
        $payid=$_POST['hinhthuctt'];
        $name=$_POST['name'];
        $email=$_POST['email'];

        $phone=$_POST['phone'];
        $ngaygiao=date("Y-m-d",strtotime($_POST['ngaygiao']));
        $address=$_POST['diachinhan'];
        $a=new Models_ShoppingCart();
        $a->insertorder($this->userid,$name,$payid,$email,$phone,$ngaygiao,$address);
        $id=$a->getoid();
        foreach ($id as $key)
        {
            $oid=$key->oid;
        }

       foreach($_SESSION['cart'] as $key=>$value)
        {


            $list=$a->getlistcart($key);
            $pid=$key;
            $qty=$value;
            foreach($list as $key=>$value)
            {
                $price=$value->price;


            }


           $a->insertdetail($pid,$oid,$qty,$price);
        }
        $_SESSION['cart']='';
        echo '<script type="text/javascript">alert("Chúc mừng bạn đã đặt hàng thành công!! Chúng tôi sẽ liên hệ với bạn trong thời gian sớm nhất"); location.href="'.PATH.'"</script>';


    }

}
